﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin
{
    public partial class ListShow : System.Web.UI.MasterPage
    {
        ListPage adminListPage = null;
        //
        public string SQL;
        protected int listCount;
        int pageSize;
        protected string clientID, bodyCssClass = "listBody";
        public bool IsSuccess = true;
        public string ErrorString = "";

        //public Entity.Base.PageEntity pageEntity = new Entity.Base.PageEntity();

        /// <summary>
        /// 目前第N页
        /// </summary>
        protected int currentPage
        {
            get { return DataTypePlus.requestInt(tb_currentPage.Text); }
            set { tb_currentPage.Text = DataTypePlus.requestString(value); }
        }
        /// <summary>
        /// 排序字段
        /// </summary>
        public string orderBy
        {
            get { return tb_orderBy.Text; }
            set { tb_orderBy.Text = value; }
        }
        /// <summary>
        /// 排序规则
        /// </summary>
        public string descasc
        {
            get { return tb_descasc.Text; }
            set { tb_descasc.Text = value; }
        }
        /// <summary>
        /// 页面的基础sql
        /// </summary>
        public string BasicSQL = "";
        /// <summary>
        /// 搜索sql
        /// </summary>
        public string SearchSQL = "";
        /// <summary>
        /// 关键字段
        /// </summary>
        public string KeyCol = "id";
        /// <summary>
        /// 添加和编辑后边跟的参数
        /// </summary>
        public string QueryString = "";
        /// <summary>
        /// 返回按钮链接
        /// </summary>
        public string GoBackHref = "";
        public string EmptyStr = "暂时没有任何内容";
      
        /// <summary>
        /// 要合计的列名的List
        /// </summary>
        public List<SumColumn> ListSumColumn = new List<SumColumn>();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            adminListPage = Page as ListPage;
            clientID = this.ClientID;
            pageSize = DataTypePlus.requestInt(tb_pageSize.Text);
            Security.chkIsLogined();
            if (!IsPostBack)
            {
                page_request();
                page_Default();
            }
            SQL_build();
        }
        /// <summary>
        /// 获取页面URL get过来的参数
        /// </summary>
        void page_request()
        {
            //ViewState["key"] = DataType.requestString(Request.QueryString["key"]);
            //
            if (DataTypePlus.requestInt(Request.QueryString["currentPage"]) > 0) currentPage = DataTypePlus.requestInt(Request.QueryString["currentPage"]);
            if (DataTypePlus.requestString(Request.QueryString["orderBy"]) != "") orderBy = DataTypePlus.requestString(Request.QueryString["orderBy"]);
            if (DataTypePlus.requestString(Request.QueryString["descasc"]) != "") descasc = DataTypePlus.requestString(Request.QueryString["descasc"]);

            //初始化搜索表单
            string searchBind = "";
            for (int i = 0; i < Request.QueryString.Count; i++)
            {
                string thisKey = Request.QueryString.Keys[i];
                if (thisKey.IndexOf("search_") > -1 && Request.QueryString[i] != "")
                {
                    if (searchBind != "") searchBind += ",";
                    searchBind += "{id:'" + clientID + "_cph_search_" + thisKey + "',val:'" + Request.QueryString[i] + "'}";
                }
            }
            if (searchBind != "") ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "search_bind([" + searchBind + "]); ", true);

            
        }
        /// <summary>
        /// 初始化页面控件
        /// </summary>
        void page_Default()
        {
            //tb_key.Text = key;
            //
            //tb_currentPage.Text = DataType.requestString(ViewState["currentPage"]);
            //tb_orderBy.Text = ViewState["orderBy"].ToString();
            //tb_descasc.Text = ViewState["descasc"].ToString();
        }
        /// <summary>
        /// 构造SQL
        /// </summary>
        void SQL_build()
        {

            SQL = BasicSQL;
            SearchSQL = "";
            adminListPage.buildSQL_Page_List_search();//构造页面搜索表单SearchSQL

            //SQL = StringPlus.SQLand(BasicSQL, SearchSQL);
            SQL =StringPlus.SQLand(BasicSQL,  SearchSQL);
            //string key = tb_key.Text;
            //if (key != "") SQL += "Title like '%" + key + "%' ";
            //
            if (orderBy == "")
            {
                orderBy = KeyCol;
                descasc = "desc";
            }
            ////数量
            //listCount = adminListPage.get_Page_RecordCount(SQL.ToString());
            //数量,合计
            listCount = 0;
            //DataSet DataSetALL = adminListPage.get_Page_DataSet(SQL, "", 1, 99999999);
            DataSet DataSetALL = adminListPage.get_Page_DataSet(SQL, orderBy + " " + descasc, 1, 99999999);
            if (DataSetALL.Tables.Count > 0) listCount = DataSetALL.Tables[0].Rows.Count;

            foreach (SumColumn sumColumn in ListSumColumn)
            {
                decimal _sum = 0;
                if (DataSetALL.Tables.Count > 0) foreach (DataRow dr in DataSetALL.Tables[0].Rows) _sum += DataTypePlus.requestDecimal(dr[sumColumn.Name]);
                switch (sumColumn.Type)
                {
                    case EnumHelper.SumType.Int: sumColumn.SumResult = DataTypePlus.requestInt(_sum).ToString(); break;
                    case EnumHelper.SumType.Double: sumColumn.SumResult = _sum.ToString(); break;
                    case EnumHelper.SumType.Money: sumColumn.SumResult = StringPlus.showMoney(_sum); break;
                    case EnumHelper.SumType.Coin: sumColumn.SumResult = StringPlus.showCoin(_sum); break;
                }
            }

            //DataSet ds = adminListPage.get_DataSet(buildSql(DataTypePlus.requestString(SQL)));
            //DataTable dt = new DataTable();
            //if (ds.Tables.Count > 0) dt = ds.Tables[0];
            //else dt = adminListPage.get_DataTable(buildSql(DataTypePlus.requestString(SQL)));
            //listCount = dt.Rows.Count;
            //if (listCount > 0)
            //{
            //    foreach (SumColumn sumColumn in ListSumColumn)
            //    {
            //        decimal _sum = 0;
            //        foreach (DataRow dr in dt.Rows) _sum += DataTypePlus.requestDecimal(dr[sumColumn.Name]);
            //        switch (sumColumn.Type)
            //        {
            //            case EnumHelper.SumType.Int: sumColumn.SumResult = DataTypePlus.requestInt(_sum).ToString(); break;
            //            case EnumHelper.SumType.Double: sumColumn.SumResult = _sum.ToString(); break;
            //            case EnumHelper.SumType.Money: sumColumn.SumResult = StringPlus.showMoney(_sum); break;
            //            case EnumHelper.SumType.Coin: sumColumn.SumResult = StringPlus.showCoin(_sum); break;
            //        }
            //    }
            //}
            //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "page_bind_SumColum(" + StringPlus.Object2Json4Js(ListSumColumn) + "); ", true);
        }
        /// <summary>
        /// 绑定列表数据
        /// </summary>
        public void Page_Bind()
        {
            //DataSet ds = adminListPage.get_Page_DataSet(DataTypePlus.requestString(SQL), orderBy + " " + descasc, currentPage * pageSize + 1, (currentPage + 1) * pageSize);
            //pageEntity.Condition = buildSql(DataTypePlus.requestString(SQL));
            //pageEntity.Order = descasc == "desc" ? 0 : 1;
            //pageEntity.OrderColumn = orderBy;
            //pageEntity.PageIndex = currentPage + 1;
            //pageEntity.PageSize = pageSize;
            //rp.DataSource = adminListPage.get_Page_List(pageSize, currentPage + 1, orderBy, descasc == "desc" ? 0 : 1, buildSql(DataTypePlus.requestString(SQL)));
            rp.DataSource = adminListPage.get_Page_DataSet(DataTypePlus.requestString(SQL), orderBy + " " + descasc, currentPage * pageSize + 1, (currentPage + 1) * pageSize);
            rp.DataBind();

            //DataSet ds = adminListPage.get_Page_DataSet(pageSize,currentPage+1,orderBy,descasc=="desc"?0:1, buildSql( DataTypePlus.requestString(SQL)));
            //DataTable dt = new DataTable();
            //if (ds != null && ds.Tables.Count > 0) dt = ds.Tables[0];
            //else dt = adminListPage.get_Page_DataTable(pageSize,currentPage+1,orderBy,descasc=="desc"?0:1, buildSql( DataTypePlus.requestString(SQL)));
            ////else dt = adminListPage.get_Page_DataTable(buildSql(DataTypePlus.requestString(SQL)), orderBy + " " + descasc, currentPage * pageSize + 1, (currentPage + 1) * pageSize);
            
            //if (dt.Rows.Count > 0)
            //{
            //    rp.DataSource = dt;
            //    rp.DataBind();
                
            //}
            ////listCount =dt.Rows.Count;
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "page_bind_ok(" + StringPlus.Object2Json4Js(ListSumColumn) + ");", true);
        }

        /// <summary>
        /// 点击搜索按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void bt_search_Click(object sender, EventArgs e)
        {
            currentPage = 0;
            SQL_build();
            //Page_Bind();
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "page_build(''); ", true);
        }


        /// <summary>
        /// 点击排序按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void bt_orderBy_Click(object sender, EventArgs e)
        {
            //ViewState["orderBy"] = tb_orderBy.Text; ;
            //ViewState["descasc"] = tb_descasc.Text; ;
            currentPage = 0;
            //Page_Bind();
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "page_build(''); ", true);
        }
        /// <summary>
        /// 点击翻页按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void bt_page_Click(object sender, EventArgs e)
        {
            //ViewState["currentPage"] = DataType.requestInt(tb_currentPage.Text);
            Page_Bind();
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void bt_bat_del_Click(object sender, EventArgs e)
        {
            adminListPage.del_Page_List(tb_batIDstr.Text);
            //Page_Bind();
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "page_build('') ", true);
        }
        //
        public void set_bodyWidth(int width_)
        {
            bodyCssClass = "listBody" + width_.ToString();
        }
        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void bt_state_Click(object sender, EventArgs e)
        {
            adminListPage.state_Page_List(DataTypePlus.requestInt(tb_id.Text), DataTypePlus.requestInt(tb_state.Text));
            Page_Bind();
        }
        /// <summary>
        /// 上下移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void bt_move_Click(object sender, EventArgs e)
        {
            adminListPage.move_Page_List(DataTypePlus.requestInt(tb_id.Text), DataTypePlus.requestInt(tb_move.Text));
            Page_Bind();
        }
        string buildSql(string sql_)
        {
            sql_ = sql_.ToLower().Trim();
            if (sql_.ToLower().Replace("and", "").Replace(" ", "") == "") return "";
            else
            {
                if (sql_.Substring(0, 3) == "and") return sql_;
                else return " and " + sql_;
            }
        }
    }
}